<template>
  <div class="page-content">
    <lz-table
      :pageList="dataList"
      :pageCount="dataCount"
      v-model:pageInfo="pageInfo"
      v-bind="contentTableConfig"
      @selectionChange="handleSelectionChange"
    >
      <!-- 1. header上的插槽 -->
      <template #headerOperation>
        <el-button v-if="isCreate" type="primary" @click="handleCreateClick">新建数据</el-button>
      </template>
      <!-- 2. table里面的插槽 -->
      <!-- 2.1. 公共插槽 -->
      <template #createAt="scope">
        <span>{{ $filter.formatTime(scope.row.createAt) }}</span>
      </template>
      <template #updateAt="scope">
        <span>{{ $filter.formatTime(scope.row.updateAt) }}</span>
      </template>
      <template #operation="scope">
        <div class="operation">
          <!-- 编辑 -->
          <el-button :disabled="!isUpdate" type="primary" size="small" plain @click="handleEditClick(scope.row)">
            <el-icon><Edit /></el-icon>编辑
          </el-button>
          <!-- 删除 -->
          <el-popconfirm title="确定要删除吗？" @confirm="handleConfirmClick(scope.row)">
            <template #reference>
              <el-button :disabled="!isDelete" type="danger" size="small" plain>
                <el-icon><Delete /></el-icon>删除
              </el-button>
            </template>
          </el-popconfirm>
        </div>
      </template>
      <!-- 2.2. 动态插入不是公共插槽的剩余插槽 -->
      <template v-for="otherSlot in otherDynamicSlots" :key="otherSlot.prop" #[otherSlot.slotName]="scope">
        <slot :name="otherSlot.slotName" :row="scope.row"></slot>
      </template>
    </lz-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watchEffect } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import LzTable from '@/base-ui/table'
import { useStore } from '@/store'
import { usePermission } from '@/hooks/use-permission'

export default defineComponent({
  props: {
    contentTableConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  components: {
    Edit,
    Delete,
    LzTable
  },
  emits: ['createData', 'editData'],
  setup(props, { emit }) {
    // 双向绑定分页器的page和size
    const pageInfo = ref({ currentPage: 1, currentSize: 3 })

    const store = useStore()
    const getPageData = (info: any = {}) => {
      // 保存查询条件
      store.commit('system/saveQueryInfo', {
        ...info,
        offset: (pageInfo.value.currentPage - 1) * pageInfo.value.currentSize,
        size: pageInfo.value.currentSize
      })
      // 发起查询请求
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          ...info,
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.currentSize,
          size: pageInfo.value.currentSize
        }
      })
    }

    // 用于一开始请求数据 里面的currentPage和currentSize发生变化立刻发送请求
    watchEffect(() => {
      getPageData(store.state.system.queryInfo)
    })

    // vuex获取数据
    const dataList = computed(() => store.getters['system/pageListData'](props.pageName))
    const dataCount = computed(() => store.getters['system/pageListCount'](props.pageName))

    // 获取不是动态插槽的剩余插槽
    const otherDynamicSlots = props.contentTableConfig.propList.filter(
      (item: any) =>
        typeof item.slotName != 'undefined' &&
        item.slotName != 'createAt' &&
        item.slotName != 'updateAt' &&
        item.slotName != 'operation'
    )

    // 获取用户的操作权限
    const isCreate = usePermission(props.pageName, 'create')
    const isUpdate = usePermission(props.pageName, 'update')
    const isDelete = usePermission(props.pageName, 'delete')
    const isQuery = usePermission(props.pageName, 'query')

    const handleSelectionChange = (value: any) => {
      console.log(value)
    }

    const handleCreateClick = () => {
      emit('createData')
    }
    const handleEditClick = (item: any) => {
      emit('editData', item)
    }

    const handleConfirmClick = (item: any) => {
      store.dispatch('system/deletePageDataAction', {
        pageName: props.pageName,
        id: item.id
      })
    }

    return {
      pageInfo,
      dataList,
      dataCount,
      getPageData,
      otherDynamicSlots,
      isCreate,
      isUpdate,
      isDelete,
      isQuery,
      handleSelectionChange,
      handleCreateClick,
      handleEditClick,
      handleConfirmClick
    }
  }
})
</script>

<style scoped lang="less">
.page-content {
  border-top: 20px solid #f5f5f5;
  padding: 0 20px 20px;
}

.operation {
  display: flex;
  justify-content: center;
}
</style>
